#apt install -y libssl-dev rapidjson-dev libboost-all-dev libre2-dev libb64-dev libnuma-dev patchelf libarchive-dev zlib1g-dev
#pip3 install distro requests build
#pip3 install nvidia-pyindex tritonclient[all]
export TRITON_CONTAINER_VERSION=25.06
export TRITON_REPO_ORGANIZATION=https://gitee.com/121786404

export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/ixdcgm/include
export LIBRARY_PATH=/usr/local/ixdcgm/lib64:/usr/local/corex/lib64:$LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/ixdcgm/lib64:/usr/local/corex/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib/python3.12/dist-packages/tvm:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib/python3.12/dist-packages/torch/lib:$LD_LIBRARY_PATH

#export NVINFER_LIBRARY
#export NVINFER_PLUGIN_LIBRARY

git config --global --add safe.directory '*'

pushd server
git checkout -f r${TRITON_CONTAINER_VERSION}
./build.py -v --no-container-build \
--build-dir=`pwd`/build \
--install-dir=/opt/tritonserver \
--enable-logging \
--enable-stats \
--enable-metrics \
--enable-cpu-metrics \
--enable-gpu-metrics \
--enable-tracing \
--enable-gpu \
--endpoint=grpc \
--endpoint=http \
--repo-tag=common:r${TRITON_CONTAINER_VERSION} \
--repo-tag=core:r${TRITON_CONTAINER_VERSION} \
--repo-tag=backend:r${TRITON_CONTAINER_VERSION} \
--repo-tag=thirdparty:r${TRITON_CONTAINER_VERSION} \
--backend=ensemble:r${TRITON_CONTAINER_VERSION} \
--cache=local:r${TRITON_CONTAINER_VERSION} \
--cache=redis:r${TRITON_CONTAINER_VERSION} \
--repoagent=checksum:r${TRITON_CONTAINER_VERSION} \
--upstream-container-version=${TRITON_CONTAINER_VERSION} \
--github-organization=${TRITON_REPO_ORGANIZATION} \
-j 64
popd



pushd tensorrt_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_TENSORRT_INCLUDE_PATHS=/usr/local/corex/include \
  -DTRITON_TENSORRT_LIB_PATHS=/usr/local/corex/lib64 \
  -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
  ..
make -j
mkdir -p /opt/tritonserver/backends/tensorrt
cp libtriton_tensorrt.so /opt/tritonserver/backends/tensorrt
cd ..
popd


pushd pytorch_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
  -DTRITON_PYTORCH_ENABLE_TORCHVISION=OFF \
  -DTRITON_PYTORCH_INCLUDE_PATHS=/usr/local/lib/python3.12/dist-packages/torch/include \
  -DTRITON_PYTORCH_LIB_PATHS=/usr/local/lib/python3.12/dist-packages/torch/lib \
  ..
make -j
mkdir -p /opt/tritonserver/backends/pytorch
cp libtriton_pytorch.so /opt/tritonserver/backends/pytorch
cd ..
popd


# pushd tensorflow_backend
# git checkout -f r${TRITON_CONTAINER_VERSION}
# mkdir build
# cd build
# cmake -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
#   -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
#   -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
#   -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
#   -DTRITON_TENSORFLOW_INCLUDE_PATHS=/usr/local/lib/python3.12/dist-packages/tensorflow/include \
#   -DTRITON_TENSORFLOW_LIB_PATHS=/usr/local/lib/python3.12/dist-packages/tensorflow \
#   ..
# make -j
# mkdir -p /opt/tritonserver/backends/tensorflow
# cp libtriton_pytorch.so /opt/tritonserver/backends/tensorflow
# cd ..
# popd


pushd onnxruntime_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_ONNXRUNTIME_INCLUDE_PATHS=/usr/local/corex/include/onnxruntime \
  -DTRITON_ONNXRUNTIME_LIB_PATHS=/usr/local/corex/lib64 \
  -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
  -DTRITON_ONNXRUNTIME_DOCKER_BUILD=OFF \
  ..
make -j
mkdir -p /opt/tritonserver/backends/onnxruntime
cp libtriton_onnxruntime.so /opt/tritonserver/backends/onnxruntime
cd ..
popd


pushd dali_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_DALI_SKIP_DOWNLOAD=ON \
  -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
  -D__ILUVATAR_WORKAROUND__=1 \
  ..
make
mkdir -p /opt/tritonserver/backends/dali
cp src/libtriton_dali.so /opt/tritonserver/backends/dali
cd ..
popd


pushd python_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
      ..
make -j
mkdir -p /opt/tritonserver/backends/python
cp libtriton_python.so triton_python_backend_stub /opt/tritonserver/backends/python
cp ../src/resources/triton_python_backend_utils.py /opt/tritonserver/backends/python
cd ..
popd

pushd vllm_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir -p /opt/tritonserver/backends/vllm
cp -r src/* /opt/tritonserver/backends/vllm
popd

pushd developer_tools/server
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_BUILD_TEST=OFF \
      -DTRITON_ENABLE_EXAMPLES=OFF ..
make -j
cp libtritondevelopertoolsserver.a /opt/tritonserver/lib/
cp -r ../include/triton/developer_tools /opt/tritonserver/include/triton/
cd ..
popd


#--backend=tensorrt:r${TRITON_CONTAINER_VERSION} \
#--backend=onnxruntime:r${TRITON_CONTAINER_VERSION} \
#--backend=python:r${TRITON_CONTAINER_VERSION} \
#--backend=vllm:r${TRITON_CONTAINER_VERSION} \
#-extra-core-cmake-arg CMAKE_CUDA_ARCHITECTURES=ivcore11 \
# [--library-paths LIBRARY_PATHS] 
# [--no-core-build]
# [--repoagent REPOAGENT] 
# [--no-force-clone]
# [--ort-version ORT_VERSION]
# [--standalone-openvino-version STANDALONE_OPENVINO_VERSION]
# [--dcgm-version DCGM_VERSION] 
# [--vllm-version VLLM_VERSION] 
# [--rhel-py-version RHEL_PY_VERSION]
# -DgRPC_DIR:PATH=/usr/lib/x86_64-linux-gnu/cmake/grpc
# -Dc-ares_DIR:PATH=/usr/lib/x86_64-linux-gnu/cmake/c-ares 
# -Dabsl_DIR:PATH=/usr/lib/x86_64-linux-gnu/cmake/absl 
# -DGTEST_ROOT:PATH=/usr/lib/x86_64-linux-gnu/cmake/GTest
# -Dnlohmann_json_DIR:PATH=/usr/share/cmake/nlohmann_json
# -Dlibevhtp_DIR:PATH=/usr/lib/cmake/libevhtp
# --build-type BUILD_TYPE] 
# --target-platform TARGET_PLATFORM] 
# --target-machine TARGET_MACHINE] 
# TRITON_ENABLE_METRICS_GPU    dcgm 关闭
# TRITON_ENABLE_GPU            for shm
# TRITON_CORE_HEADERS_ONLY
# --release-version RELEASE_VERSION] 

# libgtest-dev 
# libprotobuf-dev 
# nlohmann-json3-dev l
# ibevent-dev 
# libcurl4-openssl-dev 
# libevhtp-dev 


# nm -S bin/tritonserver | c++filt  | grep HTTPMetricsServer::Handle   ON
# nm -S lib/libtritonserver.so | c++filt  | grep PollDcgmMetrics       OFF
